package cn.edu.sgu.www.mhxysy.controller.gang;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.gang.GangMemberDTO;
import cn.edu.sgu.www.mhxysy.entity.gang.GangMember;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.gang.GangMemberService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "帮派成员管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/gang_member", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class GangMemberController {

	private final GangMemberService gangMemberService;

	@Autowired
	public GangMemberController(GangMemberService gangMemberService) {
		this.gangMemberService = gangMemberService;
	}

	@ApiOperation("导出数据")
	@RequestMapping(value = "/export", method = RequestMethod.GET)
	public void exportData() throws UnsupportedEncodingException {
		gangMemberService.exportData();
	}

	@ApiOperation("导入数据")
	@RequestMapping(value = "/import", method = RequestMethod.POST)
	public JsonResult<Void> importData(MultipartFile file) throws IOException {
		gangMemberService.importData(file);

		return JsonResult.success("导入成功");
	}

	@ApiOperation("添加帮派成员")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated GangMemberDTO memberDTO) {
		gangMemberService.insert(memberDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}
	
	@ApiOperation("通过ID删除帮派成员")
	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	public JsonResult<Void> deleteById(
			@ApiParam(name = "id", value = "帮派成员ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		gangMemberService.deleteById(id);

		return JsonResult.success(CustomMessages.DELETE_SUCCESS);
	}

	@ApiOperation("通过ID修改帮派成员信息")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated GangMemberDTO memberDTO) {
		gangMemberService.updateById(memberDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("分页查询帮派成员列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
	public JsonResult<JsonPage<GangMember>> selectByPage(Pager<GangMember> pager) {
		Page<GangMember> page = gangMemberService.selectByPage(pager);

		return JsonResult.restPage(page);
	}

}